package com.anbinh.doctor.fragment.clinic.benhnhan;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import com.anbinh.doctor.R;
import com.anbinh.doctor.fragment.clinic.FragmentDoctorList;
import com.anbinh.doctor.model.DataResponse;
import com.anbinh.doctor.model.UserLogin;
import com.anbinh.doctor.request.controller.ServerController;
import com.anbinh.doctor.service.request.OnRequest;
import com.anbinh.doctor.utils.Utils;
import com.lsjwzh.widget.materialloadingprogressbar.CircleProgressBar;

public class FragmentClinicAddBieuHien extends Fragment {
	private static FragmentClinicAddBieuHien fragment = null;
	private View mView;
	private CircleProgressBar loading;
	private EditText edBangcap;
	private static boolean mIsUpdate;

	public static FragmentClinicAddBieuHien newInstance(boolean isUpdate) {
		if (fragment == null) {
			fragment = new FragmentClinicAddBieuHien();
		}
		mIsUpdate = isUpdate;
		return fragment;
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
	}

	@Override
	public void onResume() {
		super.onResume();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mView = inflater.inflate(R.layout.fragment_them_bieuhien, null);
		
		return mView;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		Utils.setOnTouchHideKeyboard(getActivity(), mView);

		
		loading = (CircleProgressBar) mView.findViewById(R.id.loadding);
		edBangcap = (EditText) mView.findViewById(R.id.edBangcap);
		edBangcap.setHint("Thêm biểu hiện");
		if (mIsUpdate) {
			edBangcap.setText(FragmentClinicBieuhien.newInstance()
					.getCurrentBieuhien().getDisease_manifestations());
		}
	}

	public void onPostBienhien(boolean isUpdate) {
		UserLogin userLogin = Utils.getAccountModel(getActivity(),
				Utils.KEY_USER);
		String token = userLogin.getData().getToken();
		String bieuhien = edBangcap.getText().toString().trim();
		if (bieuhien.isEmpty()) {
			Utils.showMessage(getActivity(),
					getResources().getString(R.string.vui_long_nhap_bieu_hien));
			return;
		}
		loading.setVisibility(View.VISIBLE);
		int id = -1;
		if (isUpdate)
			id = FragmentClinicBieuhien.newInstance().getCurrentBieuhien().getId();
		ServerController.getInstance(getActivity())
				.requestAddEditDiseaseManifestations(
						token,
						FragmentClinicListPatients.newInstance().getCurrentPatient()
								.getId(), bieuhien, id, FragmentDoctorList.newInstance().getCurrentDoctors().getId(),
						new OnRequest<DataResponse>() {

							@Override
							public void onSuccess(DataResponse data) {
								loading.setVisibility(View.INVISIBLE);

								if (data.getResponsecode() >= 0) {
									Intent intent = new Intent();
									intent.putExtra(Utils.KEY,
											Utils.ONRESUME_BIEU_HIEN);
									getActivity().setResult(Integer.MAX_VALUE,
											intent);
									 getActivity().finish();
								}

								Utils.showMessage(getActivity(),
										data.getDescription());

							}

							@Override
							public void onError(int errorCode, String message) {
								loading.setVisibility(View.INVISIBLE);

							}

						});

	}
}
